package com.ruoyi.wisdom.constants;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.wisdom.domain.IotConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 物联网常量信息
 *
 * @author ruoyi
 */
@Component
public class IotConstants {

    @Autowired
    @Qualifier("redisTemplate")
    public RedisTemplate redisTemplate;

    /**
     * config 人脸识别配置
     */
    public static final String SYSTEM_CONFIG = "ruoyi:wisdom:system_config";
    public static final String LOCAL_FACE_URL = "http://192.168.10.211:8006/";      // 本地人脸访问URL

    // 场景类型 应用场景 1 出入校门 2出入宿舍 3校门进出通用
    public static final Integer SCENE_TYPE_SCHOOL = 1;
    public static final Integer SCENE_TYPE_GENERIC = 3;
    public static final Integer SCENE_TYPE_DORMITORY = 2;

    public static final Integer TYPE_COME = 1;  // 进方向
    public static final Integer TYPE_OUT = 2;   // 出方向

    public static final Integer RECORD_PUSH_NO = 0;     // 记录未推送消息
    public static final Integer RECORD_PUSH_YES = 1;    // 记录已推送消息

    public static final Integer RECORD_SYNC_NO = 0;     // 记录已同步
    public static final Integer RECORD_SYNC_YES = 1;    // 记录未同步

    // 人脸下发状态：1添加 2添加成功 3添加失败 4下发中 -1删除 -2删除成功 -3删除失败
    public static final Integer FACE_STATUS_ADD = 1;
    public static final Integer FACE_STATUS_ADD_SUCCESS = 2;
    public static final Integer FACE_STATUS_ADD_FAIL = 3;
    public static final Integer FACE_STATUS_TO_DOING = 4;
    public static final Integer FACE_STATUS_DELETE = -1;
    public static final Integer FACE_STATUS_DELETE_SUCCESS = -2;
    public static final Integer FACE_STATUS_DELETE_FAIL = -3;

    // status_old 1 添加 -1 删除
    public static final Integer FACE_STATIC_OLD_ADD = 1;
    public static final Integer FACE_STATIC_OLD_DEL = -1;

    /**
     * 用户类型
     */
    public static final Integer USER_TYPE_TEACHER = 1;
    public static final Integer USER_TYPE_STUDENT = 2;
    public static final Integer USER_TYPE_PARENT = 3;
    public static final String USER_GROUP_STUDENT = "student";
    public static final String USER_GROUP_TEACHER = "teacher";

    /**
     * 设备参数 状态：0未使用，1在线，-1掉线
     */
    public static final int DEVICE_STATUS_DEFAULT = 0;
    public static final int DEVICE_STATUS_ONLINE = 1;
    public static final int DEVICE_STATUS_OUTLINE = -1;

    /**
     * 设备类型：1 人脸闸机 2 测温设备 3考勤摄设备 4 AI人脸考勤
     */
    public static final int DEVICE_TYPE_FACEGATE = 1;         // 人脸闸机
    public static final int DEVICE_TYPE_TEMPERATURE = 2;      // 测温设备
    public static final int DEVICE_TYPE_ATTENDANCE = 3;       // 考勤摄设备
    public static final int DEVICE_TYPE_AIfACE = 4;           // AI人脸考勤【人脸门禁】

    /**
     * 设备供应商: 0其他、 1野马、2海康、3大华
     */
    public static final int DEVICE_FACTORY_OEM = 0;
    public static final int DEVICE_FACTORY_YM = 1;
    public static final int DEVICE_FACTORY_HK = 2;
    public static final int DEVICE_FACTORY_DH = 3;

    /**
     * 相关redsi key常量
     */
    public static final String LOCAL_COMPANY = "local-service:use-company";             // 应用本地化学校数据|使用本地化模块数据
    public static final String PULL_COMPANY_USER = "local-service:pull-company-user:";   // 拉取教职工用户数据
    public static final String PULL_FACE = "local-service:pull-face:";                  // 拉取拉取人脸数据
    public static final String PULL_OPENID = "local-service:pull-openid:";              // 拉取订阅数据
    public static final String PULL_DEVICE = "local-service:pull-device:";              // 拉取设备数据
    public static final String PULL_SCENE = "local-service:pull-scene:";                // 拉取场景数据
    public static final String PULL_PARENT = "local-service:pull-parent:";              // 拉取家长数据
    public static final String PULL_RELEASE = "local-service:pull-release:";            // 拉取临时放行数据
    public static final String PULL_SETTING = "local-service:pull-setting:";            // 拉取学校基本配置数据
    public static final String PULL_USER_FACE = "local-service:pull-user-face:";        // 拉取人脸特征管理数据
    public static final String PULL_PICK_GROUP = "local-service:pull-pick-group:";      // 拉取人脸分组
    public static final String PUSH_DEVICE = "local-service:push-device";               // 推送设备数据
    public static final String PUSH_FACE = "local-service:push-face";                   // 推送人脸数据
    public static final String PUSH_RECORD = "local-service:push-record";               // 推送出入记录
    public static final String PUSH_HISTORY = "local-service:push-history";             // 推送接送历史
    public static final String PUSH_ATTENDANCE = "local-service:push-attendance";       // 推送考勤记录
    public static final String PUSH_STUDENT_MOBILE = "local-service:push-student-mobile";   // 推送学生手机号码数据
    public static final String PUSH_TEMPLATE_MESSAGE = "push-template-message";         // 微信推送消息



    /**
     * 学校数据模块队列
     */
    public static final String PULL_SCHOOL_GRADE = "local-service:pull-school-grade:";                    // 拉取学校年级数据
    public static final String PULL_SCHOOL_CLASS = "local-service:pull-school-classes:";                  // 拉取学校班级数据
    public static final String PULL_SCHOOL_RELATION = "local-service:pull-school-classes-relation:";      // 拉取学校关系ID数据
    public static final String PULL_SCHOOL_SETTING = "local-service:pull-setting:";                       // 拉取学校配置
    public static final String PULL_SCHOOL_HEADTEACHEE = "local-service:pull-school-headteacher:";        // 拉取班主任数据

    /**
     * 学生模块数据
     */
    public static final String PULL_STUDENT_INFO = "local-service:pull-student-info:";                    // 拉取学生学籍数据
    public static final String PULL_STUDENT_PARENT = "local-service:pull-student-parent:";                // 拉取学生家长数据
    public static final String PULL_STUDENT_LEAVE = "local-service:pull-student-leave:";                  // 拉取学生请假数据
    public static final String PULL_STUDENT_LEAVE_APPLY = "local-service:pull-student-leave-apply:";      // 拉取学生请假申请数据

    // 本队服务数据处理
    public static final String TASK_FACE_DETECT = "service-task:task_face_detect";      // 人脸识别队列




    /**
     * 获取系统基础参数配置
     *
     * @return
     */
    public IotConfig getIotConfig() {
        try {
            // 从 Redis 中获取存储的数据
            Object faceConfigObj = redisTemplate.opsForValue().get(SYSTEM_CONFIG);
            if (faceConfigObj == null) {
                // 查数据表
                return null;
            } else {
                String configJson = (String) faceConfigObj;
                // 使用 Jackson 进行反序列化
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.readValue(configJson, IotConfig.class);
            }
        } catch (JsonProcessingException e) {
            System.out.println("获取系统基础参数配置异常："+e.getMessage());
            return null;
        }
    }

    /**
     * 获取comId
     *
     * @return
     */
    public Long getComId() {
        IotConfig config = getIotConfig();
        return config.getComId();
    }

    /**
     * 获取corpId
     *
     * @return
     */
    public String getCorpId() {
        IotConfig config = getIotConfig();
        return config.getCorpId();
    }

    /**
     * 获取拉取学校叫职工队列键
     *
     * @return
     */
    public String getPullCompanyUer() {
        return PULL_COMPANY_USER + getCorpId();
    }

    /**
     * 获取拉取人脸队列键
     *
     * @return
     */
    public String getPullPickFace() {
        return PULL_FACE + getCorpId();
    }

    /**
     * 获取拉取订阅消息队列键
     *
     * @return
     */
    public String getPullPickOpenId() {
        return PULL_OPENID + getCorpId();
    }

    /**
     * 获取拉取设备队列键
     *
     * @return
     */
    public String getPullPickDevice() {
        return PULL_DEVICE + getCorpId();
    }

    /**
     * 获取拉取场景队列键
     *
     * @return
     */
    public String getPullPickScene() {
        return PULL_SCENE + getCorpId();
    }

    /**
     * 获取拉取接送家属队列键
     *
     * @return
     */
    public String getPullPickParent() {
        return PULL_PARENT + getCorpId();
    }

    /**
     * 获取拉取学校年级队列键
     *
     * @return
     */
    public String getPullSchoolGrade() {
        return PULL_SCHOOL_GRADE + getCorpId();
    }

    /**
     * 获取拉取学校班级队列键
     *
     * @return
     */
    public String getPullSchoolClass() {
        return PULL_SCHOOL_CLASS + getCorpId();
    }

    /**
     * 获取拉取学校班级关系ID队列键
     *
     * @return
     */
    public String getPullSchoolRelation() {
        return PULL_SCHOOL_RELATION + getCorpId();
    }

    /**
     * 获取拉取学校配置队列键
     *
     * @return
     */
    public String getPullSchoolSetting() {
        return PULL_SCHOOL_SETTING + getCorpId();
    }

    /**
     * 获取学籍队列键
     *
     * @return
     */
    public String getPullStudentInfo() {
        return PULL_STUDENT_INFO + getCorpId();
    }

    /**
     * 获取学生家长队列键
     *
     * @return
     */
    public String getPullStudentParent() {
        return PULL_STUDENT_PARENT + getCorpId();
    }

    /**
     * 获取学生请假申请队列键
     *
     * @return
     */
    public String getPullStudentLeaveApply() {
        return PULL_STUDENT_LEAVE_APPLY + getCorpId();
    }

    /**
     * 获取学生请假数据队列键
     *
     * @return
     */
    public String getPullStudentLeave() {
        return PULL_STUDENT_LEAVE + getCorpId();
    }

    /**
     * 获取班主任数据队列键
     * @return
     */
    public String getPullSchoolHeadteacher() { return PULL_SCHOOL_HEADTEACHEE + getCorpId(); }

    /**
     * 获取人脸分组数据队列键
     * @return
     */
    public String getPullPickGroup() { return PULL_PICK_GROUP + getCorpId(); }
}
